<?php

App::uses('AppController', 'Controller');

class InstitutionsController extends AppController
{
    public $name = 'Institutions';
    public $uses = array('Institution');
    
    public function beforeFilter()
    {
        parent::beforeFilter();
    }
    
    // Páginas do CMS AQUI
    function index()
    {
        
        $Institutions = $this->Institution->find('all', array('order'=>'name ASC'));
        
        //die(debug($this->Institution->getEnumValues('partner')));
        
        $this->set('Institutions', $Institutions);
    }

    function add()
    {
        $institution_types = array('Privada'=>'Privada','Publico'=>'Público');
        $institution_partner = array('Sim'=>'Sim','Nao'=>'Não');
        
        $this->set('institution_types', $institution_types);
        $this->set('institution_partner', $institution_partner);
        $enviar = false;
        if($this->request->is('post'))
        {
            if(empty($this->request->data['Institution']['cnpj']))
            {
                $enviar = true;   
            }
            else
            {
                if($this->VALIDACNPJ($this->request->data['Institution']['cnpj']) == 0)
                {
                    $envia = false;
                    $this->Session->setFlash('<div class="alert alert-error">CNPJ INVÁLIDO!</div>');
                }
                else
                {
                    $enviar = true;
                }
            }
            
            if($enviar)
            {
                if ($this->Institution->save($this->request->data))
                {
                    // Set a session flash message and redirect.
                    $this->Session->setFlash('<div class="alert alert-success">Cdastrado com Sucesso!</div>');

                }
            }
        }
    }
    function delete($id=null)
    {
        $this->Institution->delete($id);
        $this->Session->setFlash('<div class="alert alert-success"><div class="alert alert-success">Removido com sucesso!</div>');
        $this->redirect(array('action' => 'index'));
    }
    
    function edit($id)
    {
        $institution_types = array('Privada'=>'Privada','Publico'=>'Público');
        $institution_partner = array('Sim'=>'Sim','Nao'=>'Não');
        
        $this->set('institution_types', $institution_types);
        $this->set('institution_partner', $institution_partner);
        
        
        if($this->data)
        {
//            if(!isset($this->request->data['Institution']['type'])){$this->request->data['Page']['type'] = 0;}
            
            if ($this->Institution->save($this->request->data))
            {
                // Set a session flash message and redirect.
                $this->Session->setFlash('<div class="alert alert-success">Cdastrado com Sucesso!</div>');
                
            }
         }
         else
         {
            $this->data = $this->Institution->read(null,$id);
         }
        
        
    }
}
